package com.example.pdr;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class StepDetectionHandler implements SensorEventListener {

    private final SensorManager sensorManager;
    private final Sensor sensor;
    private StepDetectionListener listener;

    public StepDetectionHandler(SensorManager sensorManager) {
        this.sensorManager = sensorManager;
        this.sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
    }

    public void startListening() {
        sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

    public void stopListening() {
        sensorManager.unregisterListener(this);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_LINEAR_ACCELERATION) {
            float yValue = event.values[1];
            if (yValue > 1 && listener != null) {
                listener.onNewStepDetected(0.8f); // 默认步长为0.8米
            }
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // 空实现
    }

    public void setStepDetectionListener(StepDetectionListener listener) {
        this.listener = listener;
    }

    public interface StepDetectionListener {
        void onNewStepDetected(float stepSize);
    }
}